package mest.service.hank;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import mest.dao.hank.WarehouseProductjobSaleoutMapper;
import mest.dao.hank.WarehouseProductjobSaleoutRecordMapper;
import mest.dao.hank.WarehouseSaleoutMapper;
import mest.entity.WarehouseSaleout;
import mest.entity.warehouse_productjob_saleout;
import mest.util.ResultObj;
import mest.util.mestUtil;

/**
 * 仓库销售业务
 *@description TODO
 *@time 2019年12月21日 下午5:11:44
 *@author Twinson Wong
 *
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class WarehouseSaleoutService {

	@Resource
    private WarehouseSaleoutMapper wsMapper;

	@Resource
	private WarehouseProductjobSaleoutMapper wpsMapper;
	
	@Resource
	private WarehouseProductjobSaleoutRecordMapper wpsrMapper;
	
	@Resource
	private HttpServletRequest request;
	
	/**
	 * 获取 仓库销售表头 列表
	 * @param map
	 * @return
	 */
	public ResultObj getWSList(Map<String, Object> map) {
		// TODO Auto-generated method stub
		List<WarehouseSaleout> list = wsMapper.selectAll(map);
		int count = wsMapper.count(map);
		ResultObj resultObj = ResultObj.createSuccess();
		resultObj.setCount(count);
		resultObj.setData(list);
		resultObj.setMsg(resultObj.SELECT_SUCCESS);
		return resultObj;
	}

	public int wsUpdate(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return wsMapper.update(map);
	}

	/**
	 * 获取 仓库销售表体
	 * @param map
	 * @return
	 */
	public ResultObj getWPSList(Map<String, Object> map) {
		// TODO Auto-generated method stub
		List<warehouse_productjob_saleout> list = wpsMapper.selectAll(map);
		int count = wpsMapper.count(map);
		ResultObj resultObj = ResultObj.createSuccess();
		resultObj.setCount(count);
		resultObj.setData(list);
		resultObj.setMsg(resultObj.SELECT_SUCCESS);
		return resultObj;
	}

	/**
	 * 销售出库
	 * @param map
	 * @throws Exception 
	 */
	public void saleOutRecord(Map<String, Object> map) throws Exception {
		// TODO Auto-generated method stub
		int sum = 0;
		int i = 0;
		//更新仓库流实际出库数量   ,如果为空就没办法出库
		//并更新 仓库库存   warehousesave_manytomany  warehousesave_manytomany_record
		//没有就创建
		List<Map<String, Object>> list = (List<Map<String, Object>>)map.get("list");
		for (Map<String, Object> map2 : list) {
			map2.put("efId", map.get("userId"));
			map2.put("theirDb", map.get("theirDb"));
			map2.put("hasbeenNumber", Integer.valueOf(map2.get("thisNumber")+""));
			map2.remove("description");
			//判断存在仓库,更改库存数
			Map<String, Object> wsm = wpsMapper.existWarehouseSave(map2);
			if (wsm == null || Integer.valueOf(""+wsm.get("number")) < Integer.valueOf(""+map2.get("thisNumber")) ) {
				//为空就跳出
				//wpsMapper.insertWarehouseSave(map2);
				throw new RuntimeException("库存不足");
			} else {
				map2.put("wsmId", wsm.get("id"));
				i = wpsMapper.updateWarehouseSave(map2);
				if (i < 1) {throw new RuntimeException("更新库存失败");}
			}
			i = wpsMapper.insertWSRecord(map2);
			if (i < 1) {throw new RuntimeException("更新销售出库日志失败");}
			
			
			sum += Integer.valueOf(map2.get("thisNumber")+"");
			
			wpsMapper.update(map2);
			if (i < 1) {throw new RuntimeException("更新销售出库总数量失败");}
			
		}
		
		//更新操作日志
		wpsrMapper.insert(map);
		//更新生产单表头  product_order   saleNumber
		map.put("saleNumber", sum);
		i = wsMapper.updatePOSaleoutNumber(map);
		if (i < 1) {throw new RuntimeException("更新生产单发货数量失败");}
		
		
	}

    
}
